package cn.bearspark.article.provider.service.impl;

import cn.bearspark.article.dto.draft.DraftDTO;
import cn.bearspark.article.dto.draft.DraftSimpleDTO;
import cn.bearspark.article.dto.draft.SaveDraftDTO;
import cn.bearspark.article.provider.dao.mapper.DraftMapper;
import cn.bearspark.article.provider.dao.po.draft.DraftBasePO;
import cn.bearspark.article.provider.dao.po.draft.DraftSimplePO;
import cn.bearspark.article.provider.service.DraftService;
import cn.bearspark.common.aop.autoparse.host.AutoParseHost;
import cn.bearspark.common.dto.Page;
import cn.bearspark.common.util.ConvertBeanUtil;
import cn.bearspark.common.util.ConvertInfoUtil;
import cn.bearspark.idgenerator.enumeration.IdCodeEnum;
import cn.bearspark.idgenerator.rpc.IdGeneratorRpc;
import com.github.pagehelper.PageHelper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author f
 */
@Service
public class DraftServiceImpl implements DraftService {

    @DubboReference
    private IdGeneratorRpc idGeneratorRpc;

    private final DraftMapper draftMapper;

    public DraftServiceImpl(DraftMapper draftMapper) {
        this.draftMapper = draftMapper;
    }

    @Override
    @AutoParseHost(SaveDraftDTO.class)
    public long save(SaveDraftDTO saveDraftDTO) {
        DraftBasePO draftBasePO = ConvertBeanUtil.convert(saveDraftDTO, DraftBasePO.class);

        long draftId;
        String content = saveDraftDTO.getContent();
        Long mightDraftId = saveDraftDTO.getDraftId();
        if (mightDraftId == null) {
            // 如果没传草稿 id，则表示新增草稿
            draftId = idGeneratorRpc.generateId(IdCodeEnum.DRAFT_ID);
            draftBasePO.setDraftId(draftId);
            draftMapper.insertBase(draftBasePO);
            draftMapper.insertContent(draftId, content);
        } else {
            // 如果传草稿 id 了，则表示更新草稿
            draftId = mightDraftId;
            if (draftMapper.updateBase(draftBasePO) != 0) {
                draftMapper.updateContent(draftId, content);
            }
        }

        return draftId;
    }

    @Override
    @AutoParseHost(DraftDTO.class)
    public DraftDTO get(long draftId, long userId) {
        return ConvertBeanUtil.convert(draftMapper.get(draftId, userId), DraftDTO.class);
    }

    @Override
    public void delete(long draftId, long userId) {
        if (draftMapper.deleteBase(draftId, userId) == 0) {
            return;
        }
        draftMapper.deleteContent(draftId);
    }

    @Override
    public Page<DraftSimpleDTO> page(int pageNum, int pageSize, long userId) {
        com.github.pagehelper.Page<DraftSimplePO> draftSimplePOPage = PageHelper.startPage(pageNum, pageSize)
                .doSelectPage(() -> draftMapper.listSimple(userId));

        Page<DraftSimpleDTO> draftSimpleDTOPage = new Page<>();
        draftSimpleDTOPage.setTotal(draftSimplePOPage.getTotal());
        draftSimpleDTOPage.setRecords(ConvertInfoUtil.convertList(draftSimplePOPage.getResult(), DraftSimpleDTO.class));

        return draftSimpleDTOPage;
    }
}
